package com.umbrella.worker.service.impl;

import java.util.Calendar;
import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.umbrella.worker.dao.WAccountRecordMapper;
import com.umbrella.worker.dao.WSupplierAccountMapper;
import com.umbrella.worker.dto.AccountRecordDO;
import com.umbrella.worker.dto.SupplierAccountDO;
import com.umbrella.worker.entity.WAccountRecord;
import com.umbrella.worker.entity.WAccountRecordExample;
import com.umbrella.worker.entity.WSupplierAccount;
import com.umbrella.worker.entity.WSupplierAccountExample;
import com.umbrella.worker.query.AccountRecordQuery;
import com.umbrella.worker.query.SupplierAccountQuery;
import com.umbrella.worker.result.ResultDO;
import com.umbrella.worker.result.ResultSupport;
import com.umbrella.worker.service.ISupplierAccountService;
import com.umbrella.worker.util.BeanUtilsExtends;
import com.umbrella.worker.util.StringUtil;

@Service("supplierAcountService")
public class SupplierAcountServiceImpl extends BaseServiceImpl implements ISupplierAccountService {
	
	private static Logger logger = Logger.getLogger(SupplierAcountServiceImpl.class);
	
	@Autowired
	private WSupplierAccountMapper supplierAccountMapper;
	@Autowired
	private WAccountRecordMapper accountRecordMapper;
	
	@Transactional
	@Override
	public ResultDO createRecrod(AccountRecordDO accountRecordDO) {
		
		WAccountRecord wsa = new WAccountRecord();
		
		ResultSupport result = BeanUtilsExtends.copy(wsa, accountRecordDO);
		
		if(!result.isSuccess()) {
			return result;
		}
		
		int recordNum = -1;
        
		wsa.setCreateTime(Calendar.getInstance().getTime());
		wsa.setDatalevel(1);
		wsa.setStatus(0);
		wsa.setModifiTime(Calendar.getInstance().getTime());
		wsa.setModifiAuthor(wsa.getCreateAuthor());
		
		try {
			recordNum = accountRecordMapper.insertSelective(wsa);
		} catch (Exception e) {
			result.setSuccess(false);
			result.setErrorMsg(ResultDO.SYSTEM_EXCEPTION_ERROR_MSG);
			result.setErrorCode(ResultDO.SYSTEM_EXCEPTION_ERROR);
			logger.error("[obj:supplierAccount][opt:create][msg:" + e.getMessage()
			+ "]");
			e.printStackTrace();
			return result;
		}
		
		if(recordNum < 1) {
			result.setSuccess(false);
			return result;
		}	
		
		result.setModel(ResultDO.FIRST_MODEL_KEY, wsa.getId());
		
		return result;
	}
	
	@Transactional
	@Override
	public ResultDO create(SupplierAccountDO supplierAccountDO) {
		
		WSupplierAccount wsa =new WSupplierAccount();
		
		ResultSupport result = BeanUtilsExtends.copy(wsa, supplierAccountDO);
		
		if(!result.isSuccess()) {
			return result;
		}
		
		int recordNum = -1;
        
		wsa.setCreateTime(Calendar.getInstance().getTime());
		wsa.setDatalevel(1);
		wsa.setStatus(0);
		wsa.setModifiTime(Calendar.getInstance().getTime());
		wsa.setModifiAuthor(wsa.getCreateAuthor());
		
		try {
			recordNum = supplierAccountMapper.insertSelective(wsa);
		} catch (Exception e) {
			result.setSuccess(false);
			result.setErrorMsg(ResultDO.SYSTEM_EXCEPTION_ERROR_MSG);
			result.setErrorCode(ResultDO.SYSTEM_EXCEPTION_ERROR);
			logger.error("[obj:supplierAccount][opt:create][msg:" + e.getMessage()
			+ "]");
			e.printStackTrace();
			return result;
		}
		
		if(recordNum < 1) {
			result.setSuccess(false);
			return result;
		}	
		
		result.setModel(ResultDO.FIRST_MODEL_KEY, wsa.getId());
		
		return result;
	}

	@Transactional
	@Override
	public ResultDO modify(SupplierAccountDO supplierAccountDO) {
		WSupplierAccount supplierAccount = new WSupplierAccount();

		ResultSupport result = BeanUtilsExtends.copy(supplierAccount, supplierAccountDO);
		// 拷贝失败
		if (!result.isSuccess()) {
			return result;
		}
		supplierAccount.setModifiTime(Calendar.getInstance().getTime());
		int recordNum = -1;
		try {
			recordNum = supplierAccountMapper.updateByPrimaryKey(supplierAccount);
		} catch (Exception e) {
			result.setSuccess(false);
			result.setErrorCode(ResultDO.SYSTEM_EXCEPTION_ERROR);
			result.setErrorMsg(ResultDO.SYSTEM_EXCEPTION_ERROR_MSG);
			logger.error("[obj:supplierAccount][opt:modifi][msg:" + e.getMessage()
					+ "]");
			return result;
		}
		if (recordNum < 1) {
			result.setSuccess(false);
		}

		return result;
	}
	
	@Transactional
	@Override
	public ResultDO updateStatus(SupplierAccountDO SupplierAccountDO) {
		// TODO Auto-generated method stub
		return null;
	}

	@Transactional
	@Override
	public ResultDO remove(int supplierAccountId) {
		// TODO Auto-generated method stub
		return null;
	}
	
	@Transactional
	@Override
	public ResultDO get(int supplierAccountId) {

		ResultSupport result = new ResultSupport();
		
		WSupplierAccount supplierAcount = null;
		if(!StringUtil.isGreatOne(supplierAccountId)) {
			 result.setSuccess(false);
			 return result;
		} 
		
		try {
			supplierAcount = supplierAccountMapper.selectByPrimaryKey(supplierAccountId);
		} catch (Exception e) {
			result.setSuccess(false);
	        result.setErrorCode(ResultDO.SYSTEM_EXCEPTION_ERROR);
	        result.setErrorMsg(ResultDO.SYSTEM_EXCEPTION_ERROR_MSG);
	        logger.error("[obj:supplierAccount][opt:get][msg:"+e.getMessage()+"]");
	        return result;
		}
		
		SupplierAccountDO supplierAccountDO = getSupplierAccountDO(supplierAcount);
		if(supplierAccountDO != null) {
		} else {
			result.setSuccess(false);
	        result.setErrorCode(ResultDO.SYSTEM_EXCEPTION_ERROR);
	        result.setErrorMsg(ResultDO.SYSTEM_EXCEPTION_ERROR_MSG);
			return result;
		}

		result.setModel(ResultSupport.FIRST_MODEL_KEY, supplierAccountDO);
		return result;
	}

	@Transactional
	@Override
	public ResultDO list(SupplierAccountQuery supplierAccountQuery) {

		ResultSupport result = new ResultSupport();
		
		WSupplierAccountExample example = new WSupplierAccountExample();
		WSupplierAccountExample.Criteria c = example.createCriteria();
	
		
		if(StringUtil.isNotEmpty(supplierAccountQuery.getSupplierName())) {
			c.andWANameEqualTo(supplierAccountQuery.getSupplierName());
		}
		/**?if(StringUtil.isGreatOne(needQuery.getMemberId())) {
			c.andWMemberIdEqualTo(needQuery.getMemberId());
		}*/
		if(StringUtil.isNotEmpty(supplierAccountQuery.getOrderByClause())) {	
			example.setOrderByClause(" " + supplierAccountQuery.getOrderByClause() + " " + supplierAccountQuery.getSort());
		} else {
			example.setOrderByClause(" CREATE_TIME DESC");
		}
		
		c.andDatalevelEqualTo(1);
		
		if(supplierAccountQuery.isPage()) {
			long count = 0;
			try {
				count = supplierAccountMapper.countByExample(example);
			} catch (Exception e) {
				result.setSuccess(false);
		        result.setErrorCode(ResultDO.SYSTEM_EXCEPTION_ERROR);
		        result.setErrorMsg(ResultDO.SYSTEM_EXCEPTION_ERROR_MSG);
		        e.printStackTrace();
		        logger.error("[obj:supplierAccount][opt:get][msg:"+e.getMessage()+"]");
		        return result;
			}
			result.setModel(ResultSupport.SECOND_MODEL_KEY, count);
			int pageNO = supplierAccountQuery.getPageNO();
			if(pageNO > 0) {
				pageNO = pageNO -1;
			}
			String pageByClause = " limit " + (pageNO * supplierAccountQuery.getPageRows())
					+ "," + supplierAccountQuery.getPageRows();
			
			example.setPageByClause(pageByClause);
		}
		
		List<WSupplierAccount> list = null;
		
		try {
			list = supplierAccountMapper.selectByExample(example);
		} catch (Exception e) {
			result.setSuccess(false);
	        result.setErrorCode(ResultDO.SYSTEM_EXCEPTION_ERROR);
	        result.setErrorMsg(ResultDO.SYSTEM_EXCEPTION_ERROR_MSG);
	        logger.error("[obj:supplierAccount][opt:get][msg:"+e.getMessage()+"]");
	        return result;
		}
		
		List<SupplierAccountDO> needList = getWSupplierAccountDOList(list);

		result.setModel(ResultSupport.FIRST_MODEL_KEY, needList);
		
		return result;
	}

	@Transactional
	@Override
	public ResultDO listAccountRecord(AccountRecordQuery accountRecordQuery) {

		ResultSupport result = new ResultSupport();
		
		WAccountRecordExample example = new WAccountRecordExample();
		WAccountRecordExample.Criteria c = example.createCriteria();
	
		if(StringUtil.isGreatOne(accountRecordQuery.getwType())) {
			c.andWTypeEqualTo(accountRecordQuery.getwType());
		}
		
		c.andDatalevelEqualTo(1);
		
		if(accountRecordQuery.isPage()) {
			long count = 0;
			try {
				count = accountRecordMapper.countByExample(example);
			} catch (Exception e) {
				result.setSuccess(false);
		        result.setErrorCode(ResultDO.SYSTEM_EXCEPTION_ERROR);
		        result.setErrorMsg(ResultDO.SYSTEM_EXCEPTION_ERROR_MSG);
		        e.printStackTrace();
		        logger.error("[obj:supplierAccount][opt:get][msg:"+e.getMessage()+"]");
		        return result;
			}
			result.setModel(ResultSupport.SECOND_MODEL_KEY, count);
			int pageNO = accountRecordQuery.getPageNO();
			if(pageNO > 0) {
				pageNO = pageNO -1;
			}
			String pageByClause = " limit " + (pageNO * accountRecordQuery.getPageRows())
					+ "," + accountRecordQuery.getPageRows();
			
			example.setPageByClause(pageByClause);
		}
		
		List<WAccountRecord> list = null;
		
		try {
			list = accountRecordMapper.selectByExample(example);
		} catch (Exception e) {
			result.setSuccess(false);
	        result.setErrorCode(ResultDO.SYSTEM_EXCEPTION_ERROR);
	        result.setErrorMsg(ResultDO.SYSTEM_EXCEPTION_ERROR_MSG);
	        logger.error("[obj:supplierAccount][opt:get][msg:"+e.getMessage()+"]");
	        return result;
		}
		
		List<AccountRecordDO> list2 = getAccountRecordDOList(list);

		result.setModel(ResultSupport.FIRST_MODEL_KEY, list2);
		
		return result;
	}
	

}
